package com.zhima.util;

import com.zhima.enums.JudgerTypeEnums;
import com.zhima.handler.AbstractJudgerHandler;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * @author : 芝麻
 * @date : 2023-09-07 09:47
 **/
@Component
public class JudgerManager implements ApplicationContextAware {
    private static final Map<JudgerTypeEnums, AbstractJudgerHandler> JUDGER_HANDLER_MAP = new HashMap<>();

    public static AbstractJudgerHandler getJudger(JudgerTypeEnums judgerTypeEnums) {
        return JUDGER_HANDLER_MAP.get(judgerTypeEnums);
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        Map<String, AbstractJudgerHandler> judgerHandlersMap = applicationContext.getBeansOfType(AbstractJudgerHandler.class);
        for (AbstractJudgerHandler abstractJudgerHandler : judgerHandlersMap.values()) {
            JUDGER_HANDLER_MAP.put(abstractJudgerHandler.getJudgerType(), abstractJudgerHandler);
        }
    }
}
